﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;

public class PuzzleDragHelperAnchor : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{

    public int id;
    public Image mask;
    public Vector2Int gridPos;
    public PuzzleDragHelper helper;
    public int left;
    public int right;
    public int buttom;

    [HideInInspector]
    public RectTransform cachedTransform;
    private void Awake()
    {
        mask = GetComponent<Image>();
        cachedTransform = this.transform as RectTransform;
    }

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        helper.pointingAnchor = id;
        mask.color = Color.green;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        helper.pointingAnchor = -1;
        mask.color = Color.white;
    }

    public void SetGridPos(Vector2Int pos)
    {
        gridPos = pos;

        if (pos.x == 1)
        {
            left = -1;
        }
        else
        {
            left = id - 1;
        }

        if (pos.x >= helper.layout.x)
        {
            right = -1;
        }
        else
        {
            right = id + 1;
        }

        if (pos.y >= helper.layout.y)
        {
            buttom = -1;
        }
        else
        {
            var buttom_pos = pos + Vector2Int.up;
            buttom = (buttom_pos.y - 1) * helper.layout.x + buttom_pos.x - 1;
        }

    }
}
